package com.samskievert.cactusboom;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Font;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;

import com.threerings.media.image.BufferedMirage;
import java.awt.image.BufferedImage;

/**
 * Displays a "potential move" cursor to the player.
 */
public class CursorSprite extends CactusSprite
{
    public CursorSprite ()
    {
        super(new CBObject.Cactus(), new BufferedMirage(new BufferedImage(48,48,BufferedImage.TYPE_3BYTE_BGR)));
        _comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
    }

    public void setColor (int color)
    {
        _cactus.owner = color;
    }
    
    public void fixCactus(int rightNum)
    {
        _cactus.fixNumSpikes(rightNum);
    }

    public void setPosition (int x, int y, CBLogic logic, CBObject game)
    {
        //mouse movement while the room was loading was throwing null pointer exceptions
        if(_cactus != null && game != null){

            logic.setState(game);
            
            _cactus.x = x / SIZE;
            _cactus.y = y / SIZE; 
            _legal = logic.isLegalMove(_cactus,1);
            moveType = logic.getMoveType(_cactus,1);
            overlap = logic.getOverlap(game.pieces, _cactus.x, _cactus.y);
            
            //System.out.println("_cactus.spikes = " + _cactus.spikes.length);
            //if we're adding spikes, we need to tell the "ghost" how many spikes to draw
            if(moveType == 2 && overlap != null){
                
                //fixes the cursor's spikecount
                if(overlap.spikes.length != _cactus.spikes.length){
                    _cactus.spikes = new int[overlap.spikes.length];
                }
                
                //i'm refraining from using the normal addSpike because
                //the client still thinks NUMSPIKES = 4...so it would reset the
                //cursor's spikes[] to length 4
                
                int lowestEmpty = -1;
                for(int i = 0; i < _cactus.spikes.length; i++)
                {
                    _cactus.spikes[i] = overlap.spikes[i];
                    if(overlap.spikes[i] == -1 && lowestEmpty == -1){
                        lowestEmpty = i;
                    }
                }
                
                //fill in that lowest empty slot so they know what they're getting
                //as long as this guy isn't full(which should only happen for one frame
                if(lowestEmpty != -1) _cactus.spikes[lowestEmpty] = _cactus.owner;
                }else{
                //otherwise, don't draw any spikes at all  
                for(int i = 0; i < _cactus.spikes.length; i++)
                {
                    _cactus.spikes[i] = -1;              
                }
            }
        
        updateCactus(_cactus);
        }
    }

    public CBObject.Cactus getPiece ()
    {
        return _cactus;
    }

    @Override // from CactusSprite
    public void paint (Graphics2D gfx)
    {
     if (_legal) {
     gfx.setColor(Color.black);
     gfx.fillRect(_bounds.x,_bounds.y,_bounds.width,_bounds.height);
        //drawing was a little wonky, so disabled for now
      
          Composite ocomp = gfx.getComposite();
           //the centerpoint of the circle
           int px = _bounds.x + _bounds.width/2, py = _bounds.y + _bounds.height/2;

           if(moveType != 3){
               gfx.setComposite(_comp);
               //super.paint(gfx);
               Font font =  new Font("arial",0,18);
               gfx.setFont(font);
               gfx.setColor(Color.white);
               //if movetype 1, cost is 5 points, if movetype 2, cost is 1 point
               //gfx.drawString(moveType == 1 ? "" + CBObject.CACTUSCOST : "" + CBObject.SPIKECOST ,px-5,py+6);
               gfx.setComposite(ocomp);
           }
          
       } 
        
  }
    
   public CBObject.Cactus overlap;
   public int moveType;
   public int quadrant;
   protected boolean _legal = false;
   protected AlphaComposite _comp;
}

